package com.nulp.psycho.db.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.nulp.psycho.db.ConnectionManager;
import com.nulp.psycho.db.dao.BaseDao;
import com.nulp.psycho.db.mapping.Converter;
import com.nulp.psycho.db.model.Result;

public class ResultDao implements BaseDao{

	private final String ADD_RESULT = "INSERT INTO result (user_id, test_id, param, value) VALUES (?,?,?,?)";
	private final String GET_RESULT_BY_ID = "SELECT * FROM result WHERE id = ?";
	private final String REMOVE_RESULT = "DELETE FROM result WHERE id = ?";
	private final String UPDATE_RESULT = "UPDATE result SET user_id=?, test_id=?, param=?, value=? WHERE id = ?";
	
	
	@Override
	public <T> void create(T element) {
		Result temp = (Result) element;
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(ADD_RESULT)) {
			preparedStatement.setInt(1, temp.getUserId());
			preparedStatement.setInt(2, temp.getTestId());
			preparedStatement.setString(3, temp.getParam());
			preparedStatement.setInt(4, temp.getValue());
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
		} finally {
			ConnectionManager.getInstance().release(connection);
		}		
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T read(Integer id) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		Result result = null;
		try (PreparedStatement statement = connection
				.prepareStatement(GET_RESULT_BY_ID)) {
			statement.setInt(1, id);
			ResultSet rs = statement.executeQuery();
			if (rs.next())
				result = Converter.<Result> fromResaultSetToObject(rs,
						Result.class);
		} catch (SQLException e) {
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) result;	
	}

	@Override
	public void delete(Integer id) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(REMOVE_RESULT)) {
			preparedStatement.setInt(1, id);
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
		} finally {
			ConnectionManager.getInstance().release(connection);
		}		
	}

	@Override
	public <T> void update(T element) {
		Result temp = (Result) element;
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(UPDATE_RESULT)) {
			preparedStatement.setInt(5, temp.getId());
			preparedStatement.setInt(1, temp.getUserId());
			preparedStatement.setInt(2, temp.getTestId());
			preparedStatement.setString(3, temp.getParam());
			preparedStatement.setInt(4, temp.getValue());
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
		} finally {
			ConnectionManager.getInstance().release(connection);
		}		
	}
	
}
